<?php


/* 公共工具方法类 */
namespace App\Library;

use App\Library\Redis\RedisObj;
use Illuminate\Support\Facades\Auth;

class Helper
{

    /**
     * @return bool  //判断是否是线上环境
     */
    public static function isProduction()
    {
        if(isset($_SERVER['SITE_ENV']) && $_SERVER['SITE_ENV'] == 'production'){
            return 1;
        }
        return 0;
    }

    // 通过生日计算年龄
    public static function getAgeByBirthday($birthday)
    {
        $age = strtotime($birthday);
        if ($age === false) {
            return false;
        }
        list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));
        $now = strtotime("now");
        list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));
        $age = $y2 - $y1;
        if ((int)($m2 . $d2) < (int)($m1 . $d1)) {
            $age -= 1;
        }

        return $age;
    }

    public static function checkSgAble($hospitalId)
    {
        if (Helper::isProduction()) {
            $hospitalConf = config('shanggongProduction.hospitals');
        } else {
            $hospitalConf = config('shanggongTesting.hospitals');
        }

        if (array_key_exists('hospital_' . $hospitalId, $hospitalConf)) {
            return 1;
        }

        return 0;
    }

    public static function getClientIp() {
        static $ip = null;
        if ($ip) {
            return $ip;
        }
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CLIENTIP'])) {
            $ip = $_SERVER['HTTP_CLIENTIP'];
        } elseif (isset($_SERVER['HTTP_X_REAL_IP'])) {
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        } elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $pos = strpos($ip, '|');
        if ($pos) {
            $ip = substr($ip, 0, $pos);
        }
        $ip = trim($ip);
        return $ip;
    }
}
